package cn.jxwazx.sheinspringboot.config;

import cn.jxwazx.sheinspringboot.holder.UserHolder;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动填充日期 时间
 * @author LoveHuaHua
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "createBy", Long.class, UserHolder.getId());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "lastUpdateBy", Long.class, UserHolder.getId());
        this.strictInsertFill(metaObject, "lastUpdateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "removeFlag", Integer.class,0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        FieldUtil.setFieldValue(metaObject.getOriginalObject(), "lastUpdateBy", UserHolder.getId());
        FieldUtil.setFieldValue(metaObject.getOriginalObject(), "lastUpdateTime", LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateBy", Long.class, UserHolder.getId());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
